package mainFragment.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import https.handler.ErrorPageHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.adapter.RMGXLvAdapter;
import mainFragment.bean.HotCollegeBean;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/2/20.
 * 高校列表
 */
@ContentView(R.layout.remengaoxiao_activity)
public class Colleges extends BaseActivity implements View.OnClickListener{
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_search)
    private ImageView common_search;
    @ViewInject(R.id.remengaoxiao_listView)
    private ListView remengaoxiao_listView;
    @ViewInject(R.id.rmgx_load_layout)
    private MyPtrRefresh_LoadView myLoadLayout;

    private ArrayList<HotCollegeBean.CollegeListBean> arryList;
    private RMGXLvAdapter rmgxLvAdapter;
    private String title;
    private int page;

    private final int INIT = 0;
    private final int LOAD = 1;

    private ErrorPageHandler myHandler = new ErrorPageHandler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    if (msg.arg1 == INIT) {
                        Gson gson = new Gson();
                        String json = (String) msg.obj;
                        HotCollegeBean hotCollegeBean = gson.fromJson(json, HotCollegeBean.class);
                        List<HotCollegeBean.CollegeListBean> hotCollegeBeenList = hotCollegeBean.getCollegeList();
                        if (hotCollegeBeenList != null) {
                            arryList.clear();
                            for (int i = 0; i < hotCollegeBeenList.size(); i++) {
                                arryList.add(hotCollegeBeenList.get(i));
                            }
                        }
                    } else if (msg.arg1 == LOAD) {
                        Gson gson = new Gson();
                        String json = (String) msg.obj;
                        HotCollegeBean hotCollegeBean = gson.fromJson(json, HotCollegeBean.class);
                        List<HotCollegeBean.CollegeListBean> hotCollegeBeenList = hotCollegeBean.getCollegeList();
                        if (hotCollegeBeenList != null) {
                            if (hotCollegeBeenList.size() > 0) {
                                for (int i = 0; i < hotCollegeBeenList.size(); i++) {
                                    arryList.add(hotCollegeBeenList.get(i));
                                }
                            } else {
                                page--;
                            }
                        }
                    }
                    rmgxLvAdapter.notifyDataSetChanged();
                    break;
                case Constant.ERROR_CODE_FINISH:
                    myLoadLayout.OnRefreshComplete();
                    break;
                default:
                    break;
            }

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
        initListView();
        getDataFromInternet(page = 1, INIT,true);
    }

    private void initData() {
        title = getResources().getString(R.string.main_page_item1);
        arryList = new ArrayList<>();
    }

    private void initView() {
        common_title.setText(title);
        common_back.setOnClickListener(this);
        common_search.setOnClickListener(this);
    }

    private void initListView() {
        rmgxLvAdapter = new RMGXLvAdapter(Colleges.this, arryList);
        remengaoxiao_listView.setAdapter(rmgxLvAdapter);
        myLoadLayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInternet(++page, LOAD,false);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return false;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

            }
        });
        remengaoxiao_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent();
                i.putExtra("id", arryList.get(position).getId());
                i.putExtra("name", arryList.get(position).getSchool_name());
                i.setClass(Colleges.this, CollegeIndex.class);
                Colleges.this.startActivity(i);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                Colleges.this.finish();
                break;
            case R.id.common_search:
                break;
            default:
                break;
        }
    }

    private void getDataFromInternet(int page, int flag,boolean waitingDialog) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("page", String.valueOf(page));
        new InterNetController(this, Constant.HOTCOLLEGE, myHandler, hashMap, flag,waitingDialog);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
